package com.problem;

public class Rotate {

    public void rotate(int[][] matrix) {
        //先转置
        for (int i=0;i<matrix.length;i++) {
            for (int j=i+1;j<matrix[i].length;j++) {
                matrix[i][j] +=matrix[j][i];
                matrix[j][i] = matrix[i][j]-matrix[j][i];
                matrix[i][j] -=matrix[j][i];
            }
        }
        //再行反转
        for (int i=0;i<matrix.length;i++) {
            //int [] arr = matrix[i];
            for (int j=0;j<matrix[i].length/2;j++) {
                swap(matrix[i],j,matrix[i].length-1-j);
            }
        }
    }

    private void swap(int[] nums,int i, int j) {
        nums[i] += nums[j];
        nums[j] = nums[i]-nums[j];
        nums[i] = nums[i]-nums[j];
    }
}
